package com.example.letgo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.letgo.Fragment.TripFragment;
import com.example.letgo.global.Constant;
import com.example.letgo.model.Story;
import com.example.letgo.model.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by cc on 2018/2/27.
 */

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button login_btn;
    private Button register_btn;


    User user = new User();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        login_btn = findViewById(R.id.login);
        register_btn = findViewById(R.id.login_register);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
        register_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }




//    向服务器请求数据并验证帐号密码
    private void login() {
        username = findViewById(R.id.login_username);
        password = findViewById(R.id.login_password);


        OkGo.post("http://119.29.113.114:8080/gogogo/login")
                .params("userEmail",username.getText().toString())
                .params("userPassword",password.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
//                        Log.e("s",s);
//                        Gson gson = new Gson();
//                        Type listType = new TypeToken<LinkedList<User>>(){}.getType();
//                        LinkedList<User> list = gson.fromJson(s, listType);
//                        for (int i = 0;i<list.size();i++) {
//                            if (list.get(i).getUserEmail().equals(username.getText().toString()) & list.get(i).getUserPassword().equals(password.getText().toString())) {
//                                int userid = list.get(i).getUserId();

//                                Constant.id = u
                        if (s.equals("没有该用户")){
                            Intent intent = new Intent(LoginActivity.this, LoginActivity.class);
                            startActivity(intent);
                            Toast.makeText(LoginActivity.this, "帐号或密码错误!", Toast.LENGTH_SHORT).show();
                        } else {

                            Log.i("ID",">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>      "+s);
                            Gson gson = new Gson();
                            user = gson.fromJson(s, User.class);
                            Constant.id = user.getUserId();
                            Log.i("ID",">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>    "+Constant.id);

                            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                            startActivity(intent);
                    }
                }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Log.e("error:", String.valueOf(e));
                        Toast.makeText(LoginActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

//                            } else {
//                                Toast.makeText(LoginActivity.this, "帐号或密码错误！", Toast.LENGTH_SHORT).show();
//                            }




//        new Thread(new  Runnable() {
//            public void run() {
//                OkHttpClient okHttpClient=new OkHttpClient();
//                //服务器返回的地址
//                Request request=new Request.Builder()
//                        .url("").build();
//
//                try {
//                    Response response=okHttpClient.newCall(request).execute();
//                    //获取到数据
//                    String date=response.body().string();
//                    //把数据传入解析josn数据方法
//                    jsonJXs(date);
//
//                } catch (IOException e) {
//
//                    e.printStackTrace();
//                }
//            }
//        }).start();
//    }
////    解析返回的帐号密码
//    private void jsonJXs(String date) {
//        if(date!=null){
//            try {
//                JSONObject jsonObject = new JSONObject(date);
//                String code=jsonObject.getString("error_code");
//                if (code.equals("0")) {
//                    JSONArray json=jsonObject.getJSONArray("result");
//                    for(int i=0;i<json.length();i++){
//
//                        JSONObject object = json.getJSONObject(i);
//                        if (object.getString("username").equals(username.getText().toString())&object.getString("password").equals(password.getText().toString())){
//
//                            String userid = object.optString("userid");
//                            int id = Integer.parseInt(userid);
//                            Constant.id = id;
//
//                            Intent intent = new Intent(LoginActivity.this, TripFragment.class);
//                            startActivity(intent);
//
//
//                        }else {
//                            Toast.makeText(this, "帐号或密码错误！", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }else{
//                    Toast.makeText(this, "网络出错！", Toast.LENGTH_SHORT).show();
//
//                }
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }
//    }





